﻿using AutoMapper;
using B.S.Traceability.Myproject.Domain;
using B.S.Traceability.Myproject.ErrorCode;
using B.S.Traceability.Myproject.infrastructure.Interface;
using B.S.Traceability.MyWrite.Api.Appicant.Command.InformationCommand;
using MediatR;

namespace B.S.Traceability.MyWrite.Api.Appicant.CommandHandler.InformationCommandHandler
{
    public class AddInformationCommandHandler : IRequestHandler<AddInformationCommand, APIResult<string>>
    {
        private readonly IBaseRepository<Information> informationRepos;
        private readonly IMapper mapper;

        public AddInformationCommandHandler(IBaseRepository<Information> InformationRepos, IMapper mapper)
        {
            informationRepos = InformationRepos;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加请假申请表命令处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<string>> Handle(AddInformationCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            var model = mapper.Map<Information>(request);
            int res = informationRepos.Add(model).Result;
            if (res > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res.ToString();
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg =   APIEnum.添加失败.ToString();
                result.data = res.ToString();
            }
            return await Task.FromResult(result);
        }
    }
}
